package tas.co.in.controller.ExtensionInfo;

import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import tas.co.in.dao.ExtensionInfo.ExtensionInfo;
import tas.co.in.domain.ExtensionInfo.ExtensionInfoBean;

public class ExtensionInfoController extends MultiActionController
{
	private ExtensionInfo extensionInfo;
	
	public void setExtensionInfo(ExtensionInfo extensionInfo) 
	{
		this.extensionInfo = extensionInfo;
	}

	public ModelAndView getBufferDetails(HttpServletRequest request,HttpServletResponse response)
	{
		List<ExtensionInfoBean> resultObject = extensionInfo.getBufferExtenions();
		
		return new ModelAndView("sharedJSP/codeAllotment","resultObject",resultObject);
	}
	
	public ModelAndView getBufferCode(HttpServletRequest request,HttpServletResponse response)
	{
		ExtensionInfoBean bean = new ExtensionInfoBean();
		bean.setArea_code(request.getParameter("areacode"));
		List<ExtensionInfoBean> resultObject = extensionInfo.getBufferCode(bean);
		
		return new ModelAndView("sharedJSP/codeAllotment","resultObject",resultObject);
	}
	
	public ModelAndView setExtIP(HttpServletRequest request,HttpServletResponse response)
	{
		HttpSession session = request.getSession(true);
		
		ExtensionInfoBean bean = new ExtensionInfoBean();
		bean.setUserID(session.getAttribute("userId").toString());
		bean.setUserName(session.getAttribute("userName").toString());
		bean.setRoleID(session.getAttribute("roleId").toString());
		bean.setExt_number(request.getParameter("extencode"));
		bean.setArea_code(request.getParameter("areacode"));
		bean.setIP(request.getParameter("ip"));
		boolean resultObject = extensionInfo.setExtIP(bean);
				
		return new ModelAndView("sharedJSP/BooleanResults", "resultObject", resultObject);
	}
	
	public ModelAndView getExtIP(HttpServletRequest request,
			HttpServletResponse response)
	{
		ExtensionInfoBean bean = new ExtensionInfoBean();
		bean.setArea_code(request.getParameter("areacode"));
		List<ExtensionInfoBean> resultObject = extensionInfo.getExtIP(bean);
		return new ModelAndView("sharedJSP/ExtensionIPRecords", "resultObject", resultObject);
	}
	
	public ModelAndView delExt(HttpServletRequest request, HttpServletResponse response)
	{
		StringTokenizer items = new StringTokenizer(request.getParameter("extencode"), ",");
		boolean resultObject = extensionInfo.delExtension(items);
		System.out.println(resultObject);
		return new ModelAndView("sharedJSP/BooleanResults", "resultObject", resultObject);
	}
}